home *** CD-ROM | disk | FTP | other *** search
- Path: alpnet76.alpes-net.fr!utilisateur
- From: monnet@alpes-net.fr (Nicolas Monnet)
- Newsgroups: comp.lang.c++
- Subject: [Q] new-allocated objects within try block
- Date: Sat, 20 Jan 1996 21:47:21 +0100
- Organization: Ensieg/I.N.P.G.
- Message-ID: <monnet-2001962147210001@alpnet76.alpes-net.fr>
- NNTP-Posting-Host: alpnet76.alpes-net.fr
-
- Here's the problem I'm facing -- it's probably a bad approach, it's even
- possibly answered in some faq but...
-
- I need to allocate an object with new, and delete it in the same block,
- as if it was allocated on the stack.
- Why? Well, I'm using an abstract base class, and I have different descendents,
- and, of course, I create one of them. Example:
-
- class ABC { public: virtual void DoSomething() = 0; };
- class A : public ABC { public: virtual void DoSomething(); };
- class B : public ABC { public: virtual void DoSomething(); };
-
- (...)
- try {
- ABC *p;
- if (test) {
- ..
- p = new A;
- } else {
- ...
- p = new B;
- }
- ...
- throw sthg;
- ...
- p->DoSomething();
- delete p;
- } catch (...) {
-
- }
-
- Then, *p is not properly destroyed, isn't it?
-
- What is another way of doing it?
-
-
-
- Thanks in advance!
-
- ----
- Nicolas Monnet
- Monnet@alpes-net.fr
-